package com.knowvoyage.knowvoyageserver.controller;

import com.knowvoyage.knowvoyageserver.pojo.entity.User;
import com.knowvoyage.knowvoyageserver.pojo.vo.Result;
import com.knowvoyage.knowvoyageserver.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@Tag(name = "用户类", description = "用于用户相关操作")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @Operation(summary = "创建用户", description = "创建用户接口")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody User user) {
        return userService.insert(user);
    }

    @Operation(summary = "根据ID查询用户", description = "根据ID查询用户接口")
    @GetMapping("/selectById")
    public Result<User> selectById(@RequestParam Long userId) {
        return userService.selectById(userId);
    }

    @Operation(summary = "根据手机号查询用户", description = "根据手机号查询用户接口")
    @GetMapping("/selectByPhone")
    public Result<User> selectByPhone(@RequestParam String phone) {
        return userService.selectByPhone(phone);
    }

    @Operation(summary = "更新用户信息", description = "更新用户信息接口")
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody User user) {
        return userService.update(user);
    }

    @Operation(summary = "删除用户", description = "删除用户接口")
    @GetMapping("/delete")
    public Result<Boolean> delete(@RequestParam Long userId) {
        return userService.delete(userId);
    }

    @Operation(summary = "获取所有用户列表", description = "获取所有用户列表接口")
    @GetMapping("/selectAll")
    public Result<List<User>> selectAll() {
        return userService.selectAll();
    }

    @Operation(summary = "修改密码", description = "获取用户信息接口")
    @PostMapping("/updatePassword")
    public Result<Boolean> updatePassword(@RequestBody Map<String,String> mp) {
        Long userId = Long.valueOf(mp.get("userId"));
        String oldPassword = mp.get("oldPassword");
        String newPassword = mp.get("newPassword");
        if(oldPassword == null || newPassword == null) {
            return Result.error(400, "参数不能为空");
        }
        return userService.updatePassword(userId, oldPassword, newPassword);
    }
}
